package com.sc.nft.helper;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.sc.nft.config.fulu.FuLuConfig;
import com.sc.nft.config.fulu.FuLuHttpClient;
import com.sc.nft.config.fulu.model.FuLuBaseRequest;
import com.sc.nft.config.fulu.model.FuLuBaseResponse;
import com.sc.nft.config.fulu.model.dto.FuLuGoodsInfoDTO;
import com.sc.nft.config.fulu.model.dto.FuLuGoodsListDTO;
import com.sc.nft.config.fulu.model.dto.FuLuGoodsTemplateDTO;
import com.sc.nft.config.fulu.model.dto.FuLuOrderDirectDTO;
import com.sc.nft.config.fulu.model.request.FuLuGoodsInfoRequest;
import com.sc.nft.config.fulu.model.request.FuLuGoodsListRequest;
import com.sc.nft.config.fulu.model.request.FuLuGoodsTemplateRequest;
import com.sc.nft.config.fulu.model.request.FuLuOrderDirectRequest;
import lombok.RequiredArgsConstructor;
import lombok.experimental.UtilityClass;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Objects;

import static com.sc.nft.config.fulu.FuLuConfig.*;

/**
 * 福禄api
 *
 * @author liGen
 * @version 1.0
 * @date 2023/11/7 11:35 上午
 */
@Component
@RequiredArgsConstructor
@Slf4j
public class FuLuHelper {

    /**
     * 获取福禄api商品列表
     *
     * @return
     */
    public List<FuLuGoodsListDTO> getFuLuGoodsList(FuLuGoodsListRequest fuLuGoodsListRequest) {
        // 构建公共请求参数
        FuLuBaseRequest fuLuBaseRequest = FuLuBaseRequest.build(JSON.toJSONString(fuLuGoodsListRequest.buildParams()), goodsListMethod);
        // 请求
        FuLuBaseResponse fuLuBaseResponse = FuLuHttpClient.sendRequest(apiHttpUrl, JSON.toJSONString(fuLuBaseRequest));
        if (Objects.nonNull(fuLuBaseRequest) && StringUtils.isNotBlank(fuLuBaseResponse.getResult())) {
            // 商品列表
            List<FuLuGoodsListDTO> fuLuGoodsListDTOS = JSONArray.parseArray(fuLuBaseResponse.getResult(), FuLuGoodsListDTO.class);
            return fuLuGoodsListDTOS;
        }
        return null;
    }

    /**
     * 获取福禄api商品详情
     *
     * @return
     */
    public FuLuGoodsInfoDTO getFuLuGoodsInfo(FuLuGoodsInfoRequest fuLuGoodsInfoRequest) {
        // 构建公共请求参数
        FuLuBaseRequest fuLuBaseRequest = FuLuBaseRequest.build(JSON.toJSONString(fuLuGoodsInfoRequest.buildParams()), goodsInfoMethod);
        // 请求
        FuLuBaseResponse fuLuBaseResponse = FuLuHttpClient.sendRequest(apiHttpUrl, JSON.toJSONString(fuLuBaseRequest));
        if (Objects.nonNull(fuLuBaseRequest) && StringUtils.isNotBlank(fuLuBaseResponse.getResult())) {
            // 商品详情
            FuLuGoodsInfoDTO fuLuGoodsInfoDTO = JSON.parseObject(fuLuBaseResponse.getResult(), FuLuGoodsInfoDTO.class);
            return fuLuGoodsInfoDTO;
        }
        return null;
    }

    /**
     * 获取福禄api商品模版详情
     *
     * @return
     */
    public FuLuGoodsTemplateDTO getFuLuGoodsTemplate(FuLuGoodsTemplateRequest fuLuGoodsTemplateRequest) {
        // 构建公共请求参数
        FuLuBaseRequest fuLuBaseRequest = FuLuBaseRequest.build(JSON.toJSONString(fuLuGoodsTemplateRequest.buildParams()), goodsTemplateMethod);
        // 请求
        FuLuBaseResponse fuLuBaseResponse = FuLuHttpClient.sendRequest(apiHttpUrl, JSON.toJSONString(fuLuBaseRequest));
        if (Objects.nonNull(fuLuBaseRequest) && StringUtils.isNotBlank(fuLuBaseResponse.getResult())) {
            // 商品模版详情
            FuLuGoodsTemplateDTO fuLuGoodsTemplateDTO = JSON.parseObject(fuLuBaseResponse.getResult(), FuLuGoodsTemplateDTO.class);
            FuLuGoodsTemplateDTO templateDTO = fuLuGoodsTemplateDTO.build();
            return templateDTO;
        }
        return null;
    }

    /**
     * 获取福禄直充下单
     *
     * @return
     */
    public FuLuOrderDirectDTO fuLuOrderDirect(FuLuOrderDirectRequest fuLuOrderDirectRequest) {
        // 构建公共请求参数
        FuLuBaseRequest fuLuBaseRequest = FuLuBaseRequest.build(JSON.toJSONString(fuLuOrderDirectRequest.buildParams()), orderDirectMethod);
        // 请求
        FuLuBaseResponse fuLuBaseResponse = FuLuHttpClient.sendRequest(apiHttpUrl, JSON.toJSONString(fuLuBaseRequest));
        if (Objects.nonNull(fuLuBaseRequest) && StringUtils.isNotBlank(fuLuBaseResponse.getResult())) {
            // 直充下单结果详情
            FuLuOrderDirectDTO fuLuOrderDirectDTO = JSON.parseObject(fuLuBaseResponse.getResult(), FuLuOrderDirectDTO.class);
            return fuLuOrderDirectDTO;
        }
        return null;
    }

}
